package com.cuitrip.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;

import com.cuitrip.business.tripservice.SearchGoogleMapActivity;
import com.cuitrip.component.page.SimpleEmptyFragmentActivity;
import com.cuitrip.service.R;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.location.places.ui.PlacePicker;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;
import com.lab.utils.ToastUtil;

/**
 * Created by MiJack on 2015/11/4.
 */
public class GoogleMapController implements MapController, OnMapReadyCallback {

    private SupportMapFragment mapFragment;
    private GoogleMap mMap;
    private String mMeetingPlace;
    private String mLatitude;
    private String mLongtitude;

    @Override
    public void init(FragmentActivity activity, String meetingPlace, String latitude, String longtitude) {
        if (TextUtils.isEmpty(meetingPlace)||TextUtils.isEmpty(latitude)||TextUtils.isEmpty(longtitude)){
            return;
        }
        this.mMeetingPlace = meetingPlace;
        this.mLatitude = latitude;
        this.mLongtitude = longtitude;
        mapFragment = new SupportMapFragment();
        activity.getSupportFragmentManager().beginTransaction().replace(R.id.mapViewContainer, mapFragment).commit();
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onPlaceSelect(FragmentActivity activity, String meetingPlace, String latitude, String longtitude) {
        updateMap(activity, meetingPlace, latitude, longtitude);
    }

    @Override
    public void showPlace(Context context, double lat, double lng, String name) {
        Bundle args = new Bundle();
        args.putDouble(VALUE_LAT, lat);
        args.putDouble(VALUE_LNG, lng);
        args.putString(VALUE_NAME, name);
        SimpleEmptyFragmentActivity.startFragmentActivity(context, GoogleMapFragment.class, args);
    }

    @Override
    public void searchMap(Activity activity, String lat, String lng, String meetingPlace) {
        if (TextUtils.isEmpty(meetingPlace)){
            Intent intent = new Intent(activity, SearchGoogleMapActivity.class);
            activity.startActivityForResult(intent, PLACE_PICKER_REQUEST);
        }else{
            PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
            double latitude = Double.valueOf(lat);
            double longitude = Double.valueOf(lng);
            double delta = 0.001;
            builder = builder.setLatLngBounds(new LatLngBounds(new LatLng(latitude - delta, longitude - delta), new LatLng(latitude + delta, longitude + delta)));
            try {
                activity.startActivityForResult(builder.build(activity), PLACE_PICKER_REQUEST);
            } catch (GooglePlayServicesRepairableException e) {
                e.printStackTrace();
                ToastUtil.toast(activity.getString(R.string.feedback_google_play_not_available));
            } catch (GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();
                ToastUtil.toast(activity.getString(R.string.feedback_google_play_not_available));
            }
        }
    }
    private void updateMap(FragmentActivity activity,String meetingPlace,String latitude,String longtitude) {
        double lat=Double.valueOf(latitude);
        double lng=Double.valueOf(longtitude);
        if (TextUtils.isEmpty(meetingPlace)||TextUtils.isEmpty(latitude)||TextUtils.isEmpty(longtitude)){
            return;
        }
        this.mMeetingPlace = meetingPlace;
        this.mLatitude = latitude;
        this.mLongtitude = longtitude;
        if (mapFragment==null&&activity!=null) {
            mapFragment = new SupportMapFragment();
            activity.getSupportFragmentManager().beginTransaction().replace(R.id.mapViewContainer, mapFragment).commitAllowingStateLoss();
            mapFragment.getMapAsync(this);
        }
        if (mMap==null){
            return;
        }
        UiSettings settings = mMap.getUiSettings();
        settings.setAllGesturesEnabled(false);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 18));
        mMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
                .position(new LatLng(lat, lng)).snippet(meetingPlace));
    }
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        UiSettings settings = mMap.getUiSettings();
        settings.setAllGesturesEnabled(false);
        if (!TextUtils.isEmpty(mMeetingPlace)&&!TextUtils.isEmpty(mLatitude)&&!TextUtils.isEmpty(mLongtitude)) {
            mMap.clear();
            updateMap(mMeetingPlace,mLatitude,mLongtitude);
        }
    }

    private void updateMap(String meetingPlace, String latitude, String longtitude) {
        double lat=Double.valueOf(latitude);
        double lng=Double.valueOf(longtitude);
        UiSettings settings = mMap.getUiSettings();
        settings.setAllGesturesEnabled(false);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 18));
        mMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
                .position(new LatLng(lat, lng)).snippet(meetingPlace));
    }
}
